package AlienArena::Server::CVar;

use 5.006;
use strict;
use warnings;
our $VERSION = '0.01';

# based on CVAR_ constants in source
# USER - a user-defined (set on the fly) cvar
our @CVAR_PROPERTIES = qw(GAMEINFO SERVERINFO LATCH ARCHIVE NOSET USER);
# these flags are the prefixes in output to the cvarlist rcon command
# mappings as indicated in cvar.c::Cvar_List_f()
our %CVAR_FLAGS = (
    '-' => 'NOSET',
    'U' => 'USERINFO',
    'S' => 'SERVERINFO',
    'L' => 'LATCH',
    '*' => 'ARCHIVE'
    );


sub new {
    my $class = shift;
    my $self = {};
    $self->{_name} = shift;
    $self->{_value} = shift;
    $self->{_properties} = [@_];
    bless $self, $class;
}

sub name { shift->{_name} };

# only value has a setter
sub value {
    my $self = shift;
    return $self->{_value} = shift if @_;
    return $self->{_value};
}

sub properties { @{shift()->{_properties}} }
sub is_latched { shift->_has_property('LATCH') }
sub is_user { shift->_has_property('USER') }
sub is_userinfo { shift->_has_property('USERINFO')}
sub is_serverinfo { shift->_has_property('SERVERINFO') }
sub is_archived { shift->_has_property('ARCHIVE') }
sub is_gameinfo { shift->_has_property('GAMEINFO') }
sub is_noset { shift->_has_property('NOSET') }

sub _has_property { grep /$_[1]/, $_[0]->properties }

=head1 NAME

AlienArena::Server::CVar - Representation of an Alien Arena CVar

=head1 SYNOPSIS

    use AlienArena::Server::CVar;

    my $foo = AlienArena::Server::CVar->new();
    ...

=head1 METHODS

=head2 new($name, $value, @properties)

Properties are GAMEINFO, SERVERINFO, LATCH, ARCHIVE, NOSET,
USERINFO,USER. Listed in @AlienArena::Server::CVar::CVAR_PROPERTIES.

=head2 name

=head2 value

=head2 properties

=head2 is_latched

=head2 is_user

=head2 is_userinfo

=head2 is_serverinfo

=head2 is_archived

=head2 is_gameinfo

=head2 is_noset


=head1 AUTHOR

Mark A. Jensen, C<< <maj -at- fortinbras -dot- us> >>

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Mark A. Jensen.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of AlienArena::Server::CVar
